/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gdata.util.*;
import com.google.gdata.client.http.AuthSubUtil;
import javax.servlet.http.Cookie;

/**
 *
 * @author Flaviano
 */
public class gettoken extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, AuthenticationException, GeneralSecurityException {
        response.setContentType("text/html;charset=UTF-8");       

        String singleUseToken = AuthSubUtil.getTokenFromReply(request.getQueryString());
        String sessionToken = AuthSubUtil.exchangeForSessionToken(singleUseToken, null);

        //String nomeArquivo = request.getParameter("Nome_arquivo");// Substituida pela linha de cima
        String cookieName = "cookieToken";
        Cookie cookie = new Cookie(cookieName, sessionToken);
        cookie.setMaxAge(7 * 24 * 60 * 60); //define o tempo de vida como 7 dias (604800//segundos)
        cookie.setVersion(0); //versão 0 da especificação de cookie
        cookie.setSecure(false); //indica que o cookie deve ser transferido pelo protocolo //HTTP padrão
        cookie.setComment("Email do visitante"); //insere um comentário para o cookie
        response.addCookie(cookie); //grava o cookie na máquina do usuário
        response.sendRedirect(request.getContextPath()+"/index.jsp");
        //response.sendRedirect("http://localhost:8084"+request.getRequestURI());
    }

    // <editor-fold defaultstate="collapsed" desc="Métodos HttpServlet. Clique no sinal de + à esquerda para editar o código.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (AuthenticationException ex) {
            Logger.getLogger(gettoken.class.getName()).log(Level.SEVERE, null, ex);
        } catch (GeneralSecurityException ex) {
            Logger.getLogger(gettoken.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (AuthenticationException ex) {
            Logger.getLogger(gettoken.class.getName()).log(Level.SEVERE, null, ex);
        } catch (GeneralSecurityException ex) {
            Logger.getLogger(gettoken.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
